package com.gadgets.busi.bm02.controller;

import com.alibaba.fastjson.JSON;
import com.gadgets.busi.bm02.dto.Ck3ModifyGenreatorDTO;
import com.gadgets.busi.bm02.dto.Ck3ModifyDictAddDTO;
import com.gadgets.busi.bm02.dto.Ck3ModifyDictQueryDTO;
import com.gadgets.busi.bm02.dto.Ck3ModifyItemDTO;
import com.gadgets.busi.bm02.pojo.Ck3ModifyDict;
import com.gadgets.busi.bm02.service.ICk3Service;
import com.gadgets.framework.common.dto.HttpBaseResp;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.FileOperUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/ck3/generator")
public class Ck3ControllerImpl {

    private static final Logger logger = LoggerFactory.getLogger(Ck3ControllerImpl.class);

    @Value("${bm02.ck3.targetPath}")
    private String targetPath;

    @Autowired
    @Qualifier("ck3ServiceImpl")
    private ICk3Service ck3Service;

    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/addDict.do")
    public HttpBaseResp generatorModifyList(HttpServletRequest request, HttpServletResponse response, Ck3ModifyDictAddDTO ck3ModifyDTO){
        ck3Service.generatorCk3Modify(ck3ModifyDTO);
        return new HttpBaseResp();
    }

    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/queryDictForPage.do")
    public HttpBaseResp<Ck3ModifyDict> queryModifyList(HttpServletRequest request, HttpServletResponse response, Ck3ModifyDictQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException{
        queryDTO.addDesc("ID");
        return new HttpBaseResp<Ck3ModifyDict>(ck3Service.queryCk3ModiftDict(queryDTO, pagination), pagination);
    }

    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET},value = "/generatorModify.do")
    public void generatorModifyFile(HttpServletRequest request, HttpServletResponse response, String generatorJson) throws GadgetsServiceException{
        Ck3ModifyGenreatorDTO dto = JSON.parseObject(generatorJson, Ck3ModifyGenreatorDTO.class);
        // 根据ID正序排序
        dto.sort();
        // 查询列表
        Ck3ModifyDictQueryDTO queryDTO = new Ck3ModifyDictQueryDTO();
        List<Long> ids = new ArrayList<>();
        queryDTO.setIds(ids);
        for (Ck3ModifyItemDTO ck3ModifyItemDTO : dto.getPropertiesList()) {
            ids.add(ck3ModifyItemDTO.getId());
        }
        queryDTO.addAsc("ID");
        // 查询结果列表
        List<Ck3ModifyDict> dictList = ck3Service.queryCk3ModiftDict(queryDTO, null);
        String fileName = UUID.randomUUID().toString();
        for (int i = 0, j = i; i < dictList.size();) {
            if(Objects.equals(dictList.get(i).getId(), dto.getPropertiesList().get(j).getId())){
                // 输出结果
                FileOperUtil.writeTxtTotFile(
                    targetPath+"/"+fileName,
                    dictList.get(i).getModifyCode()+" = "+dto.getPropertiesList().get(j).getValue()
                );
                i++;
                j++;
            }else{
                // 如果对应不上，则说明存在ID没有查询到的记录
                j++;
            }
        }
    }

}
